package com.mukefile.utils;

import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author xinke.huang@hand-china.com
 * @version 1.0
 * @date 2021/2/14
 */
public class ContextUtil {
    private static final String REQUEST = "request";
    /**
     * 获取上下文
     */
    public static final ActionContext CONTEXT = ActionContext.getContext();
    /**
     * 获取sessionMap
     * @return sessionMap
     */
    public static Map<String, Object> getSessionMap(){
        return CONTEXT.getSession();
    }

    /**
     * 获取application
     * @return application
     */
    public static Map<String, Object> getApplicationMap(){
        return CONTEXT.getApplication();
    }

    /**
     * 获取requestMap
     * @return requestMap
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> getRequestMap(){
        return (Map<String, Object>) CONTEXT.get(REQUEST);
    }

    /**
     * 获取request对象
     * @return request
     */
    public static HttpServletRequest getRequest(){
        return ServletActionContext.getRequest();
    }

    /**
     * 获取response对象
     * @return response
     */
    public static HttpServletResponse getResponse(){
        return ServletActionContext.getResponse();
    }

    /**
     * 获取servletContext对象
     * @return servletContext
     */
    public static ServletContext getServletContext(){
        return ServletActionContext.getServletContext();
    }

    /**
     * 获取session对象
     * @return session
     */
    public static HttpSession getSession(){
        return ServletActionContext.getRequest().getSession();
    }
}
